package com.bawei.module_goods.adapter

import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_goods.R
import com.bawei.module_goods.databinding.ItemMallGoodsBinding
import com.bawei.module_goods.databinding.ItemMallGoodsItemBinding
import com.bawei.module_goods.entity.GoodsEntity
import com.bawei.module_goods.entity.MallGoodsInfoPageResponse
import com.bawei.module_goods.ui.GoodsDetailActivity
import com.bumptech.glide.Glide

class MallGoodsAdapter : RecyclerView.Adapter<MallGoodsAdapter.GoodsViewHolder>() {

    private val goodsList = mutableListOf<GoodsEntity>()

    fun submitList(list: List<GoodsEntity>) {
        goodsList.clear()
        goodsList.addAll(list)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GoodsViewHolder {
        val binding = ItemMallGoodsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return GoodsViewHolder(binding)
    }

    override fun getItemCount(): Int = goodsList.size

    override fun onBindViewHolder(holder: GoodsViewHolder, position: Int) {
        holder.bind(goodsList[position])
    }

    class GoodsViewHolder(private val binding: ItemMallGoodsBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(entity: GoodsEntity) {
            binding.tvGoodsTitle.setText(entity.name)
            val innerList = entity.mallGoodsInfoPageResponseList ?: emptyList<MallGoodsInfoPageResponse>()
            val idSelector: (MallGoodsInfoPageResponse) -> Int = { item ->
                val motoIds = listOf(581731, 585238, 585243, 585244, 585245)
                val clothesIds = listOf(586148, 586153)
                when {
                    motoIds.contains(item.id) || clothesIds.contains(item.id) -> item.id
                    entity.name?.contains("整车") == true -> motoIds.random()
                    entity.name?.contains("文化用品") == true -> clothesIds.random()
                    entity.name?.contains("改装件") == true -> clothesIds.random()
                    else -> clothesIds.random()
                }
            }
            val innerAdapter = MallGoodsInnerAdapter(idSelector)
            binding.recyclerViewInner.layoutManager = GridLayoutManager(binding.root.context,2)
            binding.recyclerViewInner.adapter = innerAdapter
            innerAdapter.submitList(innerList)
        }
    }
}

class MallGoodsInnerAdapter(private val idSelector: (MallGoodsInfoPageResponse) -> Int) : RecyclerView.Adapter<MallGoodsInnerAdapter.InnerViewHolder>() {
    private val data = mutableListOf<MallGoodsInfoPageResponse>()
    fun submitList(list: List<MallGoodsInfoPageResponse>) {
        data.clear()
        data.addAll(list)
        notifyDataSetChanged()
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InnerViewHolder {
        val binding = ItemMallGoodsItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return InnerViewHolder(binding)
    }
    override fun getItemCount(): Int = data.size
    override fun onBindViewHolder(holder: InnerViewHolder, position: Int) {
        holder.bind(data[position], idSelector)
    }
    class InnerViewHolder(private val binding: ItemMallGoodsItemBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: MallGoodsInfoPageResponse, idSelector: (MallGoodsInfoPageResponse) -> Int) {
            binding.tvGoodsName.text = item.title?.toUpperCase() ?: ""
            binding.tvGoodsPrice.text = item.purchasePrice.toString()
            Glide.with(binding.ivGoodsIcon.context)
                .load(item.mainImgUrl)
                .placeholder(R.drawable.ic_launcher_foreground)
                .error(R.drawable.ic_launcher_foreground)
                .into(binding.ivGoodsIcon)
            binding.root.setOnClickListener {
                val context = binding.root.context
                val idToUse = idSelector(item)
                val intent = Intent(context, GoodsDetailActivity::class.java)
                intent.putExtra("goods_id", idToUse)
                context.startActivity(intent)
            }
        }
    }
} 